# -*- coding: utf-8 -*-

import os
import logging
import sys
import json
import paramiko
# import commands
# from __future__ import unicode_literals
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect
from django.urls import reverse
# Create your views here.

file_path = '/home/autoTest2.0/Script/send_data/'


def index(request):

    # path = reverse('star:star')
    # print(path)
    print(sys.platform)
    body = request.body
    body_str = body.decode()
    logging.info(body_str)

    body_dict = json.loads(body_str)
    print(body_dict)
    python_commond = \
        'cd file_path && python sendApiWeakness.py --kafkaIp ip_replace ' \
        '--kafkaPort 9094  --minioPort 19000 --cnt 1 --host "www.apiweakness.com"'
    python_commond2 = python_commond.replace('file_path', file_path)
    kafka_ip = body_dict['kafka_ip']
    print(kafka_ip)
    python_commond3 = python_commond2.replace('ip_replace', kafka_ip)

    # 获取操作系统
    print(python_commond3)
    # if sys.platform == 'win32' or 'win64':
        # windows
    os.popen('chcp 1252')

        # 执行python命令
        # result = os.popen(python_commond)
    result = os.system(python_commond3)
        # content_Type MIME 类型
    if result == 0:
        data = {"result": "success"}
        # return HttpResponse(data, status=200)
        return JsonResponse(data, status=200)
    else:
        data = {"error_code": result}
        return HttpResponse(data, status=200, content_type='application/json')
    # elif sys.platform == 'linux*':
    #     # linux
    #     ssh_cil = paramiko.SSHClient
    #     ssh_cil.load_system_host_keys()
    #
    #     key = paramiko.AutoAddPolicy()
    #     ssh_cil.set_missing_host_key_policy(key)
    #     ssh_cil.connect()

# if __name__ == '__main__':
#     print(sys.platform)